package com.angenela.lmz.controller;

import com.angenela.lmz.constant.Constant;
import com.angenela.lmz.dto.ObjectDTO;
import com.angenela.lmz.mapper.UserMapper;
import com.angenela.lmz.pojo.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(tags = "用户相关接口")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 注册
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("注册用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    public String register(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) throws JsonProcessingException {
        ObjectDTO dto = new ObjectDTO();

        User user = userMapper.getUserByUsername(username);
        if (user != null) {
            dto.setCode(Constant.ERROR_CODE);
            dto.setMessage(Constant.USER_EXIST);
            return objectMapper.writeValueAsString(dto);
        } else {
            user = initUser(username, password);
            userMapper.addUser(user);

            dto.setData(user);
            dto.setCode(Constant.SUCCESS_CODE);
            dto.setMessage(Constant.REGISTER_SUCCESS);
        }

        return objectMapper.writeValueAsString(dto);
    }

    /**
     * 修改密码
     *
     * @param userId
     * @param password
     * @return
     */
    @PostMapping("/update/password")
    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户Id", required = true),
            @ApiImplicitParam(name = "password", value = "新的密码", required = true)
    })
    public String updatePassword(@RequestParam("userId") Integer userId, @RequestParam("password") String password) throws JsonProcessingException {
        ObjectDTO dto = new ObjectDTO();

        userMapper.updatePassword(userId, password);
        dto.setCode(Constant.SUCCESS_CODE);
        dto.setMessage("修改成功");

        return objectMapper.writeValueAsString(dto);
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    public String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) throws JsonProcessingException {
        ObjectDTO dto = new ObjectDTO();
        User user = userMapper.getUserByUsername(username);
        if (user == null) {
            dto.setCode(Constant.ERROR_CODE);
            dto.setMessage(Constant.USER_NOT_EXIST);
        } else if (user.getPassword().equals(password)) {
            dto.setCode(Constant.SUCCESS_CODE);
            dto.setMessage(Constant.LOGIN_SUCCESS);
            dto.setData(user);
        } else {
            dto.setCode(Constant.ERROR_CODE);
            dto.setMessage(Constant.LOGIN_FAILURE);
        }

        return objectMapper.writeValueAsString(dto);
    }

    public User initUser(String username, String password) {
        User user = new User();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        user.setCreateTime(format.format(new Date()));
        user.setUsername(username);
        user.setPassword(password);
        return user;
    }
}
